package com.lq.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import com.lq.pojo.CodeMessage;
import com.lq.pojo.User;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "/my")
@Api(value = "RestfulTestController测试Rest风格")
public class RestfulTestController {
	
	@ApiOperation(value = "获取用户列表",notes = "获取所有用户列表",response = User.class,httpMethod = "get")
	@RequestMapping(value = "/getuser",method = RequestMethod.GET)
	@ResponseBody
	public List<User> getUserList(){
		User user=new User(3,"小明","123456","");
		List<User>list=new ArrayList<User>();
		list.add(user);
		return list;
	}
	
	@ApiOperation(value = "注册用户",notes = "用来注册用户信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id",value = "用户编号(必填)",dataType = "Interger",required = true),
		@ApiImplicitParam(name = "name",value = "用户名称",dataType = "String"),
		@ApiImplicitParam(name = "password",value = "用户密码",dataType = "String"),
	})
	@RequestMapping(value = "/postuser",method = RequestMethod.POST)
	@ResponseBody
	public String postUser(@ModelAttribute User user) {
		Map<Integer, User>map=new HashMap<>();
		map.put(user.getId(),user);
		return CodeMessage.SUCCESS;
	}
	/**
	 * 处理"/users/{id}" 的Get请求，用来获取Url的id值得User信息
	 * url中的ID可通过@PathVariable绑定到函数的参数中
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "获取用户详细信息",notes = "根据URL的ID来获取用户详细信息")
	@ApiImplicitParam(name = "id",value = "用户编号(必填)",required = true,dataType = "int",paramType = "path")
	@RequestMapping(value = "/getuser/{id}",method = RequestMethod.GET)
	@ResponseBody
	public User getUser(@PathVariable("id") int id) {
		Map<Integer, User>map=new HashMap<Integer, User>();
		User user=new User(23,"小明","123456","");
		User user2=new User(28,"小红","123456","");
		map.put(1,user);
		map.put(2,user2);
		return map.get(id);
	}
	
	
	@ApiOperation(value = "更新用户详细信息",notes = "根据Url的id来指定更新的对象，并根据传过来的user信息来更新用户详细信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id",value = "用户编号",required = true,dataType = "Integer",paramType = "path"),
		@ApiImplicitParam(name = "name",value = "用户姓名",dataType = "String"),
		@ApiImplicitParam(name = "password",value = "用户密码",dataType = "String")
	})
	@RequestMapping(value = "/putuser/{id}",method = RequestMethod.PUT)
	@ResponseBody
	public String putUser(@PathVariable int id,@ModelAttribute User user) {
		User user2=new User();
		user2.setId(user.getId());
		user2.setName(user.getName());
		user2.setPassword(user.getPassword());
		System.out.println(user2.toString());
		return CodeMessage.SUCCESS;
	}
	
	
	
	
}
